var currentNews;

$("#news-widget").ready(function(){
  getNewsPage("/news/last");

  $('#news-widget-pagination a').click(function(){
    getNewsPage($(this).attr('href'));
    return false;
  });
});

function getNewsPage(url){
  $.getJSON(url + ".json", parseNews);
}

function parseNews(data){
  currentNews = data;
  $('#news-widget > div.content').html('');
  //$('#news-widget > div.content').append('<h4>' + currentNews.title + '</h4>');
  $('#news-widget > div.content').append(currentNews.body);
  $('#news-widget-pagination #currentPage').text(currentNews.id);

  $('#news-widget-pagination #next').css("color", "");
  $('#news-widget-pagination #prev').css("color", "");

  if (currentNews.next_id == 0){
    $('#news-widget-pagination #next').removeAttr("href");
    $('#news-widget-pagination #next').css("color", "grey");
  } else {
    $('#news-widget-pagination #next').attr("href", "/news/"+currentNews.next_id);
  }

  if (currentNews.prev_id == 0){
    $('#news-widget-pagination #prev').removeAttr("href");
    $('#news-widget-pagination #prev').css("color", "grey");
  } else {
    $('#news-widget-pagination #prev').attr("href", "/news/"+currentNews.prev_id);
  }
}

